$(function () {
    var role_datagird = $("#role_datagird");
    var role_dialog = $("#role_dialog");
    var role_add_form = $("#role_add_form");
    var permission_all_datagrid = $("#permission_all_datagrid");
    var permission_self_datagrid = $("#permission_self_datagrid");
    var menu_combotree = $("#cc");

    role_datagird.datagrid({
        url: "/role/list",
        fit: true,
        fitColumns: true,
        pagination: true,//
        border: false,//
        striped:true,
        rownumbers:true,
        sortOrder:"desc",
        singleSelect: true,//
        columns:
            [[
                {field: 'name', title: '角色名称', width: 100},
                {field: 'sn', title: '角色编码', width: 100}
            ]],
        toolbar: "#role_datagrid_toolbar"
    });

    permission_all_datagrid.datagrid({
        url: "/permission/listAll",
        columns: [[
            {field: 'name', title: '权限名称', width: 100}
        ]],
        fit: true,
        fitColumns: true,
        singleSelect: true,
        rownumbers: true,
        onDblClickRow: function (rowIndex, rowData) {
            permission_all_datagrid.datagrid("deleteRow", rowIndex);
            permission_self_datagrid.datagrid("appendRow", rowData);
        },
        //当加载成功的时候,data就是要加载的数据
        onLoadSuccess: function (data) {
            //用permissionAllData将data缓存起来
            //permissionAllData = data; 用=引用赋值不行
            //使用extend
            permissionAllData = jQuery.extend(true, {}, data);
        }
    });

    permission_self_datagrid.datagrid({
        columns: [[
            {field: 'name', title: '权限名称', width: 100}
        ]],
        fit: true,
        fitColumns: true,
        singleSelect: true,
        rownumbers: true,
        onDblClickRow: function (rowIndex, rowData) {
            permission_self_datagrid.datagrid("deleteRow", rowIndex);
            permission_all_datagrid.datagrid("appendRow", rowData);
        },
        //右边的列表加载成功时,获取列表中所有行数据的id值
        onLoadSuccess:function (rowData) {
            var ids = $.map(rowData.rows,function (item) {
                return item.id;
            });

            //获取左边的列表中的所有数据
            var leftRows = permission_all_datagrid.datagrid("getRows");

            //循环迭代左边列表中数据
            for(var i = leftRows.length - 1;i >= 0;i --){
                var leftRowId = leftRows[i].id;
                if($.inArray(leftRowId,ids) >= 0){
                    permission_all_datagrid.datagrid("deleteRow",i);
                }
            }
        }

    });

    role_dialog.dialog({
        buttons: "#role_dialog_buttons",
        closed: true
    });

    var cmdObj = {
        addRole: function () {
            //清空表单中textbox填写的内容
            role_add_form.form("clear");
            //清空右边权限列表中的权限 {total:0,rows:[]} 因为接收的是json格式,所以就手动写一个json格式的空列表
            permission_self_datagrid.datagrid("loadData", {total: 0, rows: []});
            //将前台缓存的permissionAllData缓存回左边的权限列表
            permission_all_datagrid.datagrid("loadData", permissionAllData);
            role_dialog.dialog("setTitle", "新增");
            role_dialog.dialog("open");
        },
        editRole: function () {
            //选中一行
            var selectedRow = role_datagird.datagrid("getSelected");
            if (!selectedRow) {
                $.messager.alert("提示", "老铁,请先选中一条数据", "error");
                return;
            }

            role_add_form.form("clear");
            //数据回显
            $.post("/menu/queryByRoleId",{roleId:selectedRow.id},function (data) {
                menu_combotree.combotree("setValue",data);
            })
            //表单中textbox中的数据回显
            role_add_form.form("load", selectedRow);
            //将前台缓存的permissionAllData缓存回左边的权限列表(在右边权限列表加载完成之后 再删除两边相同的行数据)
            permission_all_datagrid.datagrid("loadData", permissionAllData);
            //回显右边权限列表上的数据
            $.post("/permission/queryForRoleId", {roleId: selectedRow.id}, function (data) {
                permission_self_datagrid.datagrid("loadData",data);
            })
            role_dialog.dialog("setTitle", "编辑");
            role_dialog.dialog("open");
        },
        save: function () {
            var url = "/role/save";
            var idValue = $("#role_id").val();
            if (idValue) {
                url = "/role/edit";
            }
            role_add_form.form("submit", {
                url: url,
                //在提交之前,封装其他需要传到后台的参数
                onSubmit: function (param) {
                    //获取右边权限列表中的所有数据
                    var rows = permission_self_datagrid.datagrid("getRows");
                    for (var i = 0; i < rows.length; i++) {
                        //获取到每一行的数据
                        var row = rows[i];
                        //将每行数据封装成permission[i].id的形式
                        param['permissions[' + i + '].id'] = row.id;
                    }
                    //获取combotree中选中的菜单的值
                    var values = menu_combotree.combotree("getValues");
                    for (var i = 0;i < values.length;i ++){
                        param['menus['+ i +"].id"] = values[i];
                    }
                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("提示", "操作成功", "info", function () {
                            cmdObj.cancel();
                            role_datagird.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("提示", data.msg, "error");
                    }
                }
            })
        },
        cancel: function () {
            role_dialog.dialog("close");
        }
    };

    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })
});